package leetcode.editor.cn;

/**
 * @author daijiyong
 * @date 2021-02-19 09:01:10
 */
public class Q0002AddTwoNumbers {
//给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。 
//
// 请你将两个数相加，并以相同形式返回一个表示和的链表。 
//
// 你可以假设除了数字 0 之外，这两个数都不会以 0 开头。 
//
// 
//
// 示例 1： 
//
// 
//输入：l1 = [2,4,3], l2 = [5,6,4]
//输出：[7,0,8]
//解释：342 + 465 = 807.
// 
//
// 示例 2： 
//
// 
//输入：l1 = [0], l2 = [0]
//输出：[0]
// 
//
// 示例 3： 
//
// 
//输入：l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
//输出：[8,9,9,9,0,0,0,1]
// 
//
// 
//
// 提示： 
//
// 
// 每个链表中的节点数在范围 [1, 100] 内 
// 0 <= Node.val <= 9 
// 题目数据保证列表表示的数字不含前导零 
// 
// Related Topics 递归 链表 数学 
// 👍 5648 👎 0

    public static void main(String[] args) {
        Solution solution = new Q0002AddTwoNumbers().new Solution();
        ListNode l1 = new Q0002AddTwoNumbers().new ListNode(8);
        ListNode l2 = new Q0002AddTwoNumbers().new ListNode(9);
        System.out.println(solution.addTwoNumbers(l1, l2));
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            if (l1 == null || l2 == null) {
                return l1 == null ? l2 : l1;
            }
            ListNode header = new ListNode(-1);
            ListNode pointer = header;
            ListNode nextOfL1 = l1;
            ListNode nextOfL2 = l2;
            int carry = 0;
            while (nextOfL1 != null || nextOfL2 != null) {
                ListNode next;
                int temp;
                if (nextOfL1 == null) {
                    temp = nextOfL2.val + carry;
                    next = new ListNode(temp % 10);
                    nextOfL2 = nextOfL2.next;
                } else if (nextOfL2 == null) {
                    temp = nextOfL1.val + carry;
                    next = new ListNode(temp % 10);
                    nextOfL1 = nextOfL1.next;
                } else {
                    temp = nextOfL1.val + nextOfL2.val + carry;
                    next = new ListNode(temp % 10);
                    nextOfL1 = nextOfL1.next;
                    nextOfL2 = nextOfL2.next;
                }
                carry = temp / 10;
                header.next = next;
                header = header.next;
            }
            if (carry > 0) {
                header.next = new ListNode(carry);
            }

            return pointer.next;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            ListNode temp = this;
            StringBuilder stringBuilder = new StringBuilder();
            while (temp != null) {
                stringBuilder.append(temp.val);
                temp = temp.next;
            }
            return stringBuilder.toString();
        }
    }

}